package com.mycompany.app;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * App
 *
 */
public class App {

    public InputStream loadFile(String inputFile) throws FileNotFoundException {
        return new FileInputStream(inputFile);
    }

    /**
     * Checks if cell is part of a merged region. If yes it returns a cell which
     * contains value (the first cell).
     *
     * @param cell to check
     * @return if merged region return value cell, else return original cell
     */
    public static Cell getValueCellFromMergedRegion(Cell cell) {
        Cell valueCell = cell;

        if (cell != null) {
            Sheet sheet = cell.getSheet();
            int regionCount = sheet.getNumMergedRegions();
            for (int regionIndex = 0; regionIndex < regionCount; regionIndex++) {
                CellRangeAddress cellRangeAddress = sheet.getMergedRegion(regionIndex);
                if (cellRangeAddress.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
                    valueCell = sheet.getRow(cellRangeAddress.getFirstRow()).getCell(cellRangeAddress.getFirstColumn());
                    break;
                }
            }
        }

        return valueCell;
    }

    public DVCassette convertRow(Row row, Sheet sheet) {
        DVCassette cassette = new DVCassette();

        Cell cellNumber = row.getCell(0);
        Cell cellSubsection = row.getCell(2);
        cellNumber = getValueCellFromMergedRegion(cellNumber);
        cellSubsection = getValueCellFromMergedRegion(cellSubsection);
        String sNumber = null;


        try {
            if (cellNumber.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                int number = (int) cellNumber.getNumericCellValue();
                sNumber = Integer.toString(number);
            } else if (cellNumber.getCellType() == Cell.CELL_TYPE_STRING) {
                sNumber = cellNumber.getStringCellValue();

            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        String section = row.getCell(1).getStringCellValue();
        String subsection = cellSubsection.getStringCellValue();

        String footage = row.getCell(4).getStringCellValue();
        String format = row.getCell(5).getStringCellValue();
        String description = row.getCell(6).getStringCellValue();


        cassette.setCassetteNumber(sNumber);
        cassette.setSection(section);

        cassette.setSubsection(subsection);
        cassette.setFootage(footage);
        cassette.setFormat(format);
        cassette.setDescription(description);

        return cassette;
    }

    public List<DVCassette> loadData(InputStream inputXls) {
        List<DVCassette> dataXls = new ArrayList<DVCassette>();
        try {
            Workbook wb = new HSSFWorkbook(inputXls);
            Sheet sheet = wb.getSheetAt(0);
            for (Iterator<Row> rowIterator = sheet.rowIterator(); rowIterator.hasNext();) {
                Row row = rowIterator.next();
                DVCassette temp = convertRow(row, sheet);
                dataXls.add(temp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataXls;
    }

    public void printToOutput(List<DVCassette> data) {
        try {         
            for (DVCassette dvCas : data) {
                System.out.println(dvCas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    public void executeApp(String inputFileName) {
        JasperReportTemplate jrt = new JasperReportTemplate();
           
        try {
            InputStream InpStream = loadFile(inputFileName);
            List<DVCassette> l = loadData(InpStream);
            jrt.printToPdf(l);
            printToOutput(l);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        String inputFileName = args[0];
        App application = new App();
        application.executeApp(inputFileName);

    }
}
